<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\dao\car;


use app\dao\BaseDao;
use app\model\car\CarOrder;

/**
 * Class CarOrderDao
 * @package app\dao\car
 */
class CarOrderDao extends BaseDao
{
    protected function setModel(): string
    {
        return CarOrder::class;
    }

    /**
     * 列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public function getList(array $where = [], int $page, int $limit){
        $model = $this->getModel()->alias('o')->where('o.is_del','=',0);
        $model->join('car_type t','t.id=o.type_id','left')
            ->join('car_type_info i','i.id=t.type_info_id','left')
            ->join('base_city c1','c1.id=o.take_city_id','left')
            ->join('base_city c2','c2.id=o.return_city_id','left')
            ->join('base_place p1','p1.id=o.take_place_id','left')
            ->join('base_place p2','p2.id=o.return_place_id','left')
            ->join('base_place p3','p3.id=o.store_id','left');
        $model->field('o.*,p3.title as store_name,p3.contact_tel as store_tel,p3.start_time as store_start_time,p3.end_time as store_end_time,
            i.type_name,i.gear as type_gear,i.seat_num as type_seat_num,i.power as type_power,
            c1.city_name as take_city_name,c2.city_name as return_city_name,
            p1.title as take_place_name,p2.title as return_place_name'
        );
        $model = $this->setWheres($model,$where);
        $count = $model->count();
        $list = $model->order('o.id desc')->page($page,$limit)->select()->toArray();
        return compact('list','count');
    }

    /**
     * 搜索条件
     * @param $model
     * @param $where
     * @return mixed
     */
    public function setWheres($model,$where){
        // 关键字
        if(isset($where['keyword']) && $where['keyword']){
            if($where['field_key'] == 'all'){
                $model->where('o.order_no|o.contact_name|o.take_title|o.return_title|o.uid','like',"%{$where['keyword']}%");
            }elseif($where['field_key'] == 'uid'){
                $model->where('o.uid',$where['keyword']);
            }elseif($where['field_key'] == 'store_name'){
                $model->where('p3.title','like',"%{$where['keyword']}%");
            }elseif($where['field_key'] == 'order_no'){
                $model->where('o.order_no','like',"%{$where['keyword']}%");
            }
        }

        // 门店
        if(isset($where['place_id']) && $where['place_id']){
            $model->where('o.store_id','=',$where['place_id']);
        }
        // 状态
        if($where['status'] !== ''){
            if($where['status'] !== 'all'){
                $model->where('o.status','=',$where['status']);
            }
        }
        // UID
        if(isset($where['uid']) && $where['uid']){
            $model->where('o.uid','=',$where['uid']);
        }
        // 支付方式
        if(isset($where['pay_type']) && $where['pay_type']){
            $model->where('o.pay_type','=',$where['pay_type']);
        }
        // ids
        if(isset($where['ids']) && $where['ids']){
            $model->where('o.id','in',$where['ids']);
        }
        return $model;
    }

    /**
     * 统计订单数量
     * @param array $where
     * @return mixed
     */
    public function getOrderCount(array $where = []){
        $model = $this->getModel()->alias('o')->where('is_del',0);
        $model = $this->setWheres($model,$where);
        return $model->count();
    }

    /**
     * 导出订单
     * @param array $where
     * @return mixed
     */
    public function exportOrder(array $where = []){
        $model = $this->getModel()->alias('o')->where('o.is_del',0);
        $model->join('base_place p','p.id=o.store_id','left');
        $model->join('car_order_take t','t.id=o.take_id','left');
        $model->field('o.illegal_deposit,o.deposit,o.shifu_car_fee,o.coupon_price,o.order_no,o.pay_time,o.type_name,o.contact_name,o.contact_mobile,o.add_time,o.start_time,o.end_time,p.title as store_name,o.pay_time,o.price,t.car_no');
        $model = $this->setWheres($model,$where);
        $list = $model->select()->toArray();
        return $list;
    }
}
